import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'dart:io';

import '../entry.dart';
import '../utils/screw.dart';
import '../common/flatPanel.dart';

class SearchBoxPage extends StatelessWidget {
  final _content = TextEditingController();
  final _baiduPrefix = 'https://www.baidu.com/s?wd=';

  void _onSearch() {
    if (_content.text.isEmpty) return;

    var content = _content.text.trim();
    if (content.startsWith('#')) {
      content = content.substring(1);
      if (!content.contains(','))
        Process.start(content, []);
      else {
        var items = content.split(',');
        var item = items.first;
        items.removeAt(0);
        Process.start(item, items);
      }
    } else {
      Screw.openUrl(_baiduPrefix + content);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(Entry.SearchBox),
        actions: <Widget>[
          IconButton(
            icon: Icon(Icons.cleaning_services),
            onPressed: () {
              if (_content.text.isNotEmpty) _content.clear();
            },
          )
        ],
      ),
      body: FlatPanel(
        margin: 32,
        padding: 32,
        child: TextField(
          controller: _content,
          decoration: InputDecoration(
            icon: Icon(Icons.search),
            hintText: 'Search',
            hintStyle: TextStyle(fontSize: 18),
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: FaIcon(FontAwesomeIcons.solidPaperPlane),
        onPressed: _onSearch,
      ),
    );
  }
}
